package cn.tedu.eb_film.service.impl;

import cn.tedu.eb_film.ex.ServiceException;
import cn.tedu.eb_film.mapper.CsTDCSMapper;
import cn.tedu.eb_film.pojo.dto.CsTDCSAddNewDTO;
import cn.tedu.eb_film.pojo.dto.CsTDCSUpdateDTO;
import cn.tedu.eb_film.pojo.entity.Cinema;
import cn.tedu.eb_film.pojo.entity.CsTDCS;
import cn.tedu.eb_film.pojo.vo.CinemaHallListVO;
import cn.tedu.eb_film.pojo.vo.CinemaStandardVO;
import cn.tedu.eb_film.pojo.vo.CsTDCSListItemVO;
import cn.tedu.eb_film.pojo.vo.CsTDCSStandardVO;
import cn.tedu.eb_film.service.CsTDCSService;
import cn.tedu.eb_film.utils.ServiceCode;
import cn.tedu.eb_film.utils.StateCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
/**
 * 3D眼镜收费标准服务实现
 *
 * @author cj
 * @since 2023-03-18 18:38:25r
 */

@Service
@Slf4j
public class CsTDCSServiceImpl implements CsTDCSService {
    @Autowired
    CsTDCSMapper csTDCSMapper;

    @Override
    public void addNew(CsTDCSAddNewDTO csTDCSAddNewDTO) {
        log.debug("开始处理3D眼镜收费标准新增");

        CsTDCS csTDCS = new CsTDCS();
        BeanUtils.copyProperties(csTDCSAddNewDTO, csTDCS);
        csTDCS.setCreateTime(LocalDateTime.now());
        csTDCS.setUpdateTime(LocalDateTime.now());
        if (csTDCS.getState() == null){
            csTDCS.setState(1);
        }
        final int rows = csTDCSMapper.insert(csTDCS);
        if (rows != 1) {
            String message = "新增失败,3D眼镜收费标准数据不为1";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
        log.debug("新增完成");
    }

    @Override
    public void addNews(List<CsTDCSAddNewDTO> csTDCSAddNewDTOs) {
        log.debug("开始批量3D眼镜收费标准新增");
        List<CsTDCS> csTDCSs = new ArrayList<>();
        csTDCSAddNewDTOs.forEach(addNew -> {
            CsTDCS csTDCS = new CsTDCS();
            BeanUtils.copyProperties(addNew, csTDCS);
            csTDCS.setUpdateTime(LocalDateTime.now());
            csTDCS.setCreateTime(LocalDateTime.now());
            csTDCSs.add(csTDCS);
        });
        log.debug("开始插入到数据库");
        int rows = csTDCSMapper.insertBatch(csTDCSs);
        if (rows <= 1) {
            String message = "新增失败,新增3D眼镜收费标准数据不大于1";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
        log.debug("新增完成");
    }

    @Override
    public void deleteById(Integer id) {
        log.debug("开始根据id删除3D眼镜收费标准数据");
        final CsTDCSStandardVO standardVO = csTDCSMapper.selectById(id);
        if (standardVO == null) {
            String message = "删除失败,该3D眼镜收费标准不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
        CsTDCS csTDCS = new CsTDCS();
        csTDCS.setId(id);
        csTDCS.setState(2);
        int rows = csTDCSMapper.updateById(csTDCS);
        if (rows != 1) {
            String message = "删除失败,该3D眼镜收费标准数据不为1";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
        log.debug("删除成功");
    }

    @Override
    public void deleteByIds(List<Integer> id) {
        log.debug("开始批量删除数据");
        id.forEach(item -> {
            final CsTDCSStandardVO standardVO = csTDCSMapper.selectById(item);
            if ( standardVO == null || standardVO.getState().equals(StateCode.STATE_DELETE.getValue())) {
                String message = "删除失败,该3D眼镜收费标准数据不存在";
                log.warn(message + "该3D眼镜收费标准数据id为{}", item);
                throw new ServiceException(ServiceCode.ERROR_DELETE, message);
            }

        });

        List<Integer> ids = new ArrayList<>(id);

        final int rows = csTDCSMapper.deleteBatch(ids);
        if (rows <= 1) {
            String message = "删除失败,该电影院数据不大于1";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
        log.debug("删除成功");
    }

    @Override
    public Long count() {
        return csTDCSMapper.countAll();
    }

    @Override
    public CsTDCSStandardVO getStandardVOById(Integer id) {
        log.debug("开始处理根据Id查询数据");
         CsTDCSStandardVO standardVO = csTDCSMapper.selectById(id);
        if (standardVO == null || standardVO.getState().equals(StateCode.STATE_DELETE.getValue())) {
            String message = "查询失败,该数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_SELECT, message);
        }
        return standardVO;
    }

    @Override
    public List<CsTDCSListItemVO> list() {
        log.debug("开始处理查询所有数据");
        List<CsTDCSListItemVO> list = csTDCSMapper.selectBatch();
        list.removeIf(hall -> hall.getState().equals(StateCode.STATE_DELETE.getValue()));
        return list;
    }

    @Override
    public void update(CsTDCSUpdateDTO csTDCSUpdateDTO) {
        log.debug("开始修改编辑数据");
        final CsTDCSStandardVO standardVOById = csTDCSMapper.selectById(csTDCSUpdateDTO.getId());
        if (standardVOById == null || standardVOById.getState().equals(StateCode.STATE_DELETE.getValue())){
            String message = "修改失败,改数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }


        CsTDCS csTDCS = new CsTDCS();
        BeanUtils.copyProperties(csTDCSUpdateDTO,csTDCS);
        csTDCS.setUpdateTime(LocalDateTime.now());

        final int rows = csTDCSMapper.updateById(csTDCS);
        if (rows != 1) {
            String message = "修改失败,修改条数不为1";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
        log.debug("修改成功");
    }
}
